<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>单据管理页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="icon" href="../images/favicon.ico">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                           <label class="layui-form-label">商品名称</label>
                           <div class="layui-input-inline">
                             <input id="keyword" type="text" name="goodName" autocomplete="off" class="layui-input">
                           </div>
                        </div>
                        <div class="layui-inline">
                          <label class="layui-form-label">出入库日期</label>
                            <div class="layui-input-inline">
                              <input id="date" type="text" name="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="searchBtn" type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索 </button>
                        	<button id="addBtn" type="button" class="layui-btn">单据录入</button>
                        </div>
                    </div>
                </form>
            </div>
            <legend>出入库商品数量统计</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form id="orderSum" class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">按年统计</label>
                          	<div class="layui-input-inline">
                             	<input id="year" type="text" name="year" placeholder="yyyy" autocomplete="off" class="layui-input">
                          	</div>
                        </div>
                        <div class="layui-inline">
                          	<label class="layui-form-label">按月统计</label>
                            <div class="layui-input-inline">
                              	<input id="month" type="text" name="month" placeholder="MM" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                          	<label class="layui-form-label">按日统计</label>
                            <div class="layui-input-inline">
                              	<input id="day" type="text" name="day" placeholder="dd" autocomplete="off" class="layui-input">
                            </div>
                        </div>
          				<div class="layui-inline">
                            <button id="searchBtn1" type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 统 计 </button>
                        </div>
                    </div>
                </form>
            </div>
            <legend>统计结果</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form id="orderCount" class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">入库(kg)</label>
                          	<div class="layui-input-inline">
                             	<input id="s1" readonly type="text" name="s1" autocomplete="off" class="layui-input">
                          	</div>
                        </div>
                        <div class="layui-inline">
                          	<label class="layui-form-label">出库(kg)</label>
                            <div class="layui-input-inline">
                              	<input id="s2" readonly type="text" name="s2" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-radius layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-radius layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        
        <!-- 定义了单据新增时弹出窗口时的模板 -->
        <script type="text/html" id="orderForm">
			<form class="layui-form" action="" lay-filter="orderForm">
				<div class="layui-form-item">
    				<div class="layui-input-inline">
      					<input type="hidden" id="id" name="id">
    				</div>
  				</div>
				
				<div class="layui-form-item">
    				<label class="layui-form-label">出入库选择</label>
    				<div class="layui-input-inline">
      					<input type="radio" name="state" value="入库" title="入库">
      					<input type="radio" name="state" value="出库" title="出库">
   		 			</div>
  				</div>
				<div class="layui-form-item">
    				<label class="layui-form-label">工作人员</label>
    				<div class="layui-input-inline">
      					<input type="text" name="dpName" placeholder="请输入姓名" autocomplete="off" class="layui-input">
    				</div>
  				</div>

  				<div class="layui-form-item">
    				<label class="layui-form-label">名称</label>
    				<div class="layui-input-inline">
						<select id="goodId" name="goodId" lay-verify="required" lay-search=""></select>
    				</div>
  				</div>

				<div class="layui-form-item">
    				<label class="layui-form-label">数量</label>
    				<div class="layui-input-inline">
      					<input type="text" name="count" placeholder="请输入产品数量" autocomplete="off" class="layui-input">
    				</div>
  				</div>

				<div class="layui-form-item">
                     <label class="layui-form-label">日期</label>
                     <div class="layui-input-inline">
                         <input id="d1" type="text" name="orDate" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                     </div>
                </div>

				<div class="layui-form-item">
    				<label class="layui-form-label">单位</label>
    				<div class="layui-input-inline">
      					<input type="text" name="place" placeholder="请输入单位名称" autocomplete="off" class="layui-input">
    				</div>
  				</div>

				<div class="layui-form-item">
    				<div class="layui-input-block">
      					<button id="saveBtn" type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
					</div>
  				</div>

			</form>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    layui.use(['form', 'table', 'layer', 'laydate'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer,
            laydate = layui.laydate;
        
        /**
         *  日期渲染
         */
        laydate.render({
            elem: '#d1',
            type: 'date'
         });
        
        laydate.render({
            elem: '#date',
            type: 'date'
         });
        
        laydate.render({
            elem: '#day',
            type: 'date'
         });
        
        laydate.render({
            elem: '#month',
            type: 'month'
         });
        
        laydate.render({
           elem: '#year',
           type: 'year'
        });
        
        
        // 搜索按钮绑定事件
        $("#searchBtn").on("click", function(){
        	// table表格渲染数据
            table.render({
                elem: '#currentTableId',
                url: '/order?action=orderList', // 发送请求的地址
                where: {
                	keyword: $("#keyword").val(),
                	date: $("#date").val()
                	},
                cols: [[
                    {field: 'id', title: 'ID', width: '60', sort: true},
                    {field: 'state', title: '状态', width: '60'},
                    {field: 'name', title: '名称', width: '91'},
                    {field: 'factory', title: '厂家', width: '91'},
                    {field: 'model', title: '型号', width: '80'},
                    {field: 'specs', title: '规格', width: '131'},
                    {field: 'count', title: '数量(kg)', width: '102'},
                    {field: 'orDate', title: '日期', width: '109'},
                    {field: 'orTime', title: '填单时间', width: '163'},
                    {field: 'place', title: '单位', width: '84'},
                    {field: 'dpName', title: '工作员名', width: '89'},
                    {field: 'opName', title: '操作员名', width: '99'},
                    {title: '操作', width: '120', toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 10,
                page: true,
                skin: 'line'
            }); //end table.render
        }).click(); //end $("#searchBtn")
        
        $("#searchBtn1").on("click", function(){
        	let data = form.val("orderSum");
        	data.action = "sum";
        	data.year = $("#year").val();
        	data.month = $("#month").val();
        	data.day = $("#day").val();
        	$.post('/order', data, function(s){
        		let sIn = s.data[0].count == null ? 0 : s.data[0].count;
        		let sOut = s.data[1].count == null ? 0 : s.data[1].count;
        		$("#s1").val(sIn);
        		$("#s2").val(sOut);
        		$("#year").val("");
            	$("#month").val("");
            	$("#day").val("");
        	});
        }); //end $("#searchBtn1")

       // 新增按钮绑定事件
       $("#addBtn").on("click", function(){
    	   
    	   $.get("/order?action=goodsList", function(goods){
    		   console.log(goods);
    	   		layer.open({
    		   		type: 1,
        	   		title: "录入单据",
        	   		area: ["420px", "400px"],
        	  	 	content: $("#orderForm").html()
        		});
    	   		$.each(goods, function(i, v){
             	   $("#goodId").append("<option value="+v.id+">"+v.name+"</option>");
                }); //end $.each
          		// 渲染
          		form.render();
          		laydate.render({
                    elem: '#d1',
                    type: 'date'
                });
    	   }); // end $.get("/order?action=goodsList")
    	   
       }); // end $("#addBtn")
        
        
    // 保存按钮绑定事件
  	$("body").on("click", "#saveBtn", function(){
           // 打包form表单中的所有数据
           var data = form.val("orderForm");
           data.action = 'save';
           // 发送请求把数据推送后台
           $.post("/order", data, function(){
           	// 请求成功后  1、关窗口，2、点搜索
           	layer.closeAll();
           	$("#searchBtn").click();
           }); //end $.post
       }); //end $("#saveBtn")


        // 表格操作工具条时间
        table.on('tool(currentTableFilter)', function (obj) {
            
        	if(obj.event === 'edit'){ // 点的是编辑
        		
        		$.get("/order?action=goodsList", function(goods){
        			layer.open({
                 		type: 1,
                 		title: "编辑单据",
                 		area: ["420px", "400px"],
                 		content: $("#orderForm").html()
                 	});	
        			
        			$.each(goods, function(i, v){
                 	   $("#goodId").append("<option value="+v.id+">"+v.name+"</option>");
                    }); //end $.each
                    
                    laydate.render({
                    	elem:'#d1',
                    	type:'date'
                    });
                    
                	$.get("/order?action=edit&id="+obj.data.id, function(data){
                    	form.val("orderForm", data); //end form.val
                    }); //end $.get
        		});
                // 渲染表单
                form.render();
                laydate.render({
                	elem:'#d1',
                	type:'date'
                });
            } //end if
			if(obj.event === 'delete'){ // 点的是删除
				layer.confirm("您确定要删除该单据吗？", function(){
					// 通知后台删除该员工（发请求）
					$.post("/order", {action:'del', id:obj.data.id}, function(){
						// 关窗口
						layer.closeAll();
						// 重现渲染一遍table表格数据
						$("#searchBtn").click();
					}); //end $.post
				}); //end layer.confirm
				
          	}
        });

    });
</script>

</body>
</html>